package com.java.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class EventTest implements ActionListener{
	private JFrame frame;
	private JButton[] jbs;
	private JTextArea jta;
	
	public EventTest(){
		frame=new JFrame("Event Test!");
		jbs=new JButton[4];
		jta=new JTextArea(20,40);
		String str="东南西北";
		for(int i=0;i<str.length();i++){
			jbs[i]=new JButton(str.substring(i,i+1));
		}
		
		init();
		addEventHandler();
	}
	
	public void init(){
		frame.add(jbs[0],BorderLayout.EAST);
		frame.add(jbs[1],BorderLayout.SOUTH);
		frame.add(jbs[2],BorderLayout.WEST);
		frame.add(jbs[3],BorderLayout.NORTH);
		frame.add(jta,BorderLayout.CENTER);
	}
	
	public void addEventHandler(){
		for (int i = 0; i < jbs.length; i++) {
//			jbs[i].addActionListener(new ActionListener() {
//				public void actionPerformed(ActionEvent e) {
//					jta.append(e.getActionCommand() + "边按钮被点击！\n");
//				}
//			});
			jbs[i].addActionListener(this);
		}		
	}
	
	public void actionPerformed(ActionEvent e) {
		jta.append(e.getActionCommand() + "边按钮被点击！\n");
		
	}

	public void showMe(){
		frame.pack();
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	
	public static void main(String[] args){
		new EventTest().showMe();
	}
	

}
